package com.zntg.schedule.reNameCrmFile;

import com.zntg.bo.CrmAcceDtlBo;
import com.zntg.bo.TblobstorageBo;
import com.zntg.bo.TblobstorageSimpleBo;
import com.zntg.mapper.aiiidb.CrmAcceDtlMapper;
import com.zntg.mapper.crmdb.TblobtSorageMapper;
import com.zntg.utils.ComponentFactory;
import com.zntg.utils.CopyDirectory;
import com.zntg.utils.FileUtils;
import com.zntg.utils.string.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
**复制crm文件到crm文件，即重命名文件，crm文件是不带后缀名的，复制一份带后缀名的文件到相应的crm目录中
 */
@Component
public class ScheduledTaskRenameCrmFile {
    @Value("${template.targetPath}")
    String targetPath;
    @Value("${template.renamePath}")
    String renamePath;
    @Value("${spring.profiles.active}")
    String active;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTaskRenameCrmFile.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

   //@Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
   /* @Scheduled(cron = "0 0/3 * * * ?") */  //每1分钟
   /*@Scheduled(fixedRate = 50000000)*/
    public void testMyBatis() {
       // System.out.println("定时从app文件服务器获取所有附件信息,并同步到crm文件服务器"+active);
            try {
                CrmAcceDtlMapper crmAcceDtlMapper = ComponentFactory.getBean(CrmAcceDtlMapper.class);
                TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
                Map<String,Object> queryMap=new HashMap<>();
                List<TblobstorageSimpleBo> appFileList=tblobtSorageMapper.findAllCrmFiles(null);//app所有附件信息
                //客服app所有附件信息
                int size=0;
                if(!StringUtil.isNullOrEmpty(appFileList)){
                    size=appFileList.size();
                }
                if(size>0){
                    //System.out.println("总条数========================"+size);
                    for(int i=0;i<size;i++){
                        TblobstorageSimpleBo appFileBo=appFileList.get(i);
                        TblobstorageBo crmFileBo=new TblobstorageBo();
                        String lStorageid=appFileBo.getlStorageid();
                        crmFileBo.setlStorageid(lStorageid);
                        String cCatalog=appFileBo.getcCatalog();
                        crmFileBo.setcCatalog(cCatalog);
                        String fileUrl=appFileBo.getFileUrl();
                        String dCreateDate=appFileBo.getdCreatedate();
                        //获取原始文件存放地址
                        // String oFileUrl=ipAddress+"/"+cFilePath.replaceAll("\\\\","/")+"baccessory";
                        String appFilename=appFileBo.getcFilename();
                        String cSubKeyId=appFileBo.getcSubkeyId();
                        String fileName=lStorageid+"baccessory";
                        if(!StringUtil.isNullOrEmpty(dCreateDate)){
                            dCreateDate=dCreateDate.substring(0,10);
                        }
                        String targetFilePath=renamePath+dCreateDate+"/Tblobstorage/"+lStorageid.substring(0,lStorageid.length()-1)+"/";
                        String localFilePath=targetPath+appFileBo.getcFilepath()+"";
                        localFilePath=localFilePath.replaceAll("\\\\","/");
                        targetFilePath=targetFilePath.replaceAll("\\\\","/")+"/";
                        String srcSourPath=localFilePath+"baccessory";
                        String targetSourPath=targetFilePath+appFilename;
                        // 复制文件
                        File sourceFile=new File(srcSourPath);
                        File targetFile=new File(targetSourPath);
                        if(!targetFile.exists()){
                            if(sourceFile.exists()){
                                System.out.println("复制文件======"+sourceFile+",到======="+targetSourPath);
                                FileUtils.mkdir(targetFilePath);
                                CopyDirectory.copyFile(sourceFile,targetFile);
                            }else{
                               // System.out.println("文件不存在======"+sourceFile);
                            }
                        }


                    }
                    System.out.println("附件复制完毕========================"+size);

                }

                if (appFileList != null) {
                    //System.out.println(appFileList.size()+"条任务处理完成,其中新增 "+newCount+"条,已存在 "+oldCount+"条");
                } else {
                   // System.out.println("我什么事都不用做，等待下次再说吧");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {

            }

    }
}

